package cn.xopencode.oss.manager.permission;

import cn.xopencode.common.framework.exception.GlobalException;
import cn.xopencode.common.framework.util.CollectionUtils;

import cn.xopencode.common.framework.util.StringUtils;
import cn.xopencode.oss.rpc.permission.dto.PermissionAssignEmployeePositionDTO;
import cn.xopencode.oss.service.permission.PermissionService;
import cn.xopencode.oss.service.permission.ResourceServiceImpl;
import cn.xopencode.oss.service.permission.PositionService;
import cn.xopencode.oss.rpc.permission.dto.PermissionAssignPositionResourceDTO;
import cn.xopencode.oss.rpc.permission.dto.PermissionCheckDTO;
import cn.xopencode.oss.service.permission.bo.ResourceBO;
import cn.xopencode.userservice.rpc.user.UserRpc;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import static cn.xopencode.common.framework.exception.enums.GlobalErrorCodeConstants.FORBIDDEN;

/**
 * 权限 Manager
 */
@Service
public class PermissionManager {

    @Autowired
    private PermissionService permissionService;
    @Autowired
    private PositionService positionService;
    @Autowired
    private ResourceServiceImpl resourceService;
    @DubboReference(version = "${dubbo.consumer.UserRpc.version}")
    private UserRpc userRpc;
    /**
     * 获得職位拥有的资源编号
     *
     * @param PositionId 职务编号
     * @return 资源编号列表
     */
    public Set<String> listPositionResourceIds(String PositionId) {
        // 超级管理员，拥有所有资源
        if (positionService.hasSuperAdmin(Collections.singleton(PositionId))) {
            return CollectionUtils.convertSet(resourceService.listResources(), ResourceBO::getId);
        }
        // 非超级管理员，查询拥有资源
        return permissionService.listPositionResourceIds(PositionId);
    }

    /**
     * 赋予职务资源
     *
     * @param assignResourceDTO 赋予职务资源 DTO
     */
    public void assignPositionResource(PermissionAssignPositionResourceDTO assignResourceDTO) {
        permissionService.assignPositionResource(assignResourceDTO.getPositionId(), assignResourceDTO.getResourceIds());
    }

    /**
     * 校验职位是否拥有指定权限。
     *
     * 如果没有，则抛出 {@link cn.xopencode.common.framework.exception.ServiceException} 异常
     *
     * @param checkDTO 校验权限 DTO
     */
    public void checkPermission(PermissionCheckDTO checkDTO) {
        //校验checkDTO.getPositionId()是否为空
        if (StringUtils.isEmpty(checkDTO.getPositionId())) { // 如果没有职务，默认无法访问
            throw new GlobalException(FORBIDDEN);
        }
        List<String> positionIds=new ArrayList<>();
        positionIds.add(checkDTO.getPositionId());
        // 校验权限
        permissionService.checkPermission(positionIds, checkDTO.getPermissions());
    }

    /**
     * 将
     * @param assignPositionDTO
     */
    public void assignEmployeePosition(PermissionAssignEmployeePositionDTO assignPositionDTO) {
        /**
         * 此处的逻辑为:
         * 1、调用user-service服务，批量更新用户的positionID字段。
         */
    }

}
